package com.ruoyi.web.controller.asset;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AssetTransactionRecord;
import com.ruoyi.asset.service.IAssetTransactionRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 领用归还记录Controller
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Controller
@Slf4j
@Api("领用归还记录管理")
@RequestMapping("/asset/transactionRecord")
public class AssetTransactionRecordController extends BaseController {
    private String prefix = "asset/transactionRecord";

    @Autowired
    private IAssetTransactionRecordService assetTransactionRecordService;

    @RequiresPermissions("asset:transactionRecord:view")
    @GetMapping()
    public String transactionRecord() {
        return prefix + "/transactionRecord";
    }

    /**
     * 查询领用归还记录列表
     */
    @RequiresPermissions("asset:transactionRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetTransactionRecord assetTransactionRecord) {
        startPage();
        List<AssetTransactionRecord> list = assetTransactionRecordService.selectAssetTransactionRecordList(assetTransactionRecord);
        return getDataTable(list);
    }

    /**
     * 导出领用归还记录列表
     */
    @RequiresPermissions("asset:transactionRecord:export")
    @Log(title = "领用归还记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetTransactionRecord assetTransactionRecord) {
        List<AssetTransactionRecord> list = assetTransactionRecordService.selectAssetTransactionRecordList(assetTransactionRecord);
        ExcelUtil<AssetTransactionRecord> util = new ExcelUtil<AssetTransactionRecord>(AssetTransactionRecord.class);
        return util.exportExcel(list, "领用归还记录数据");
    }

    /**
     * 新增领用归还记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存领用归还记录
     */
    @RequiresPermissions("asset:transactionRecord:add")
    @Log(title = "领用归还记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetTransactionRecord assetTransactionRecord) {
        return toAjax(assetTransactionRecordService.insertAssetTransactionRecord(assetTransactionRecord));
    }

    /**
     * 修改领用归还记录
     */
    @RequiresPermissions("asset:transactionRecord:edit")
    @GetMapping("/edit/{transactionId}")
    public String edit(@PathVariable("transactionId") String transactionId, ModelMap mmap) {
        AssetTransactionRecord assetTransactionRecord = assetTransactionRecordService.selectAssetTransactionRecordByTransactionId(transactionId);
        mmap.put("assetTransactionRecord", assetTransactionRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存领用归还记录
     */
    @RequiresPermissions("asset:transactionRecord:edit")
    @Log(title = "领用归还记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetTransactionRecord assetTransactionRecord) {
        return toAjax(assetTransactionRecordService.updateAssetTransactionRecord(assetTransactionRecord));
    }

    /**
     * 删除领用归还记录
     */
    @RequiresPermissions("asset:transactionRecord:remove")
    @Log(title = "领用归还记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(assetTransactionRecordService.deleteAssetTransactionRecordByTransactionIds(ids));
    }

    /**
     * 根据资产编号获取资产名称
     **/
    @GetMapping("/getAssetName")
    @ResponseBody
    public AjaxResult getAssetName(String assetNumber) {
        String assetName = assetTransactionRecordService.getAssetName(assetNumber);
        return assetName == null ? new AjaxResult(AjaxResult.Type.WARN, "不存在资产信息") : new AjaxResult(AjaxResult.Type.SUCCESS, "操作成功", assetName);
    }

    @ApiOperation("归还接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "transactionId")
    )
    @PostMapping("/rest/returnAsset")
    public AjaxResult returnAsset(AssetTransactionRecord assetTransactionRecord) {
        log.info(assetTransactionRecord.toString());
        String msg = assetTransactionRecordService.returnAsset(assetTransactionRecord);
        return StringUtils.isEmpty(msg) ? new AjaxResult(AjaxResult.Type.SUCCESS, "归还成功") : new AjaxResult(AjaxResult.Type.ERROR, msg);

    }

    @ApiOperation("借出接口")
    @PostMapping("/rest/lendAsset")
    public AjaxResult lendAsset(AssetTransactionRecord assetTransactionRecord) {
        log.info(assetTransactionRecord.toString());
        String msg = assetTransactionRecordService.lendAsset(assetTransactionRecord);
        return StringUtils.isEmpty(msg) ? new AjaxResult(AjaxResult.Type.SUCCESS, "归还成功") : new AjaxResult(AjaxResult.Type.ERROR, msg);

    }


}
